<template>

  <el-dialog :title="dialogTitle" :visible.sync="showYprDia" width="983px">
    <div class="choose-ypr-container">
      <div class="choose-ypr-search-container">
        <span class="title">单位：</span>
        <el-select
          v-model="dw"
          style="width: 130px; margin-right: 30px"
          size="mini"
          clearable
          placeholder="请选择"
        >
          <el-option
            v-for="(item,index) in dwList"
            :key="index"
            :label="item"
            :value="item"
          >
          </el-option>
        </el-select>
        <el-input
          style="width: 152px; margin-right: 20px"
          size="mini"
          clearable
          v-model="courseYpr"
          placeholder="关键字搜索"
        >
          <i slot="prefix" class="el-input__icon el-icon-search"></i>
        </el-input>
        <div class="search-button">
          <img src="../../assets/assessment/icon-search.png" alt=""/>
          <span @click="showYprClick">查询</span>
        </div>
        <div class="button-list">
          <div
            class="button-item"
            v-for="(item, index) in searchNameList"
            :key="index"
            @click="delTag(index)"
          >
            <img
              class="del-icon"
              src="../../assets/assessment/approval-del.png"
              alt=""
            />
            <div>{{ item }}</div>
          </div>
        </div>
      </div>
      <div class="choose-ypr-table-container">
        <el-table
          ref="multipleTable"
          :data="yjnameList"
          border
          style="width: 100%"
          class="table-data"
          row-class-name="row-class"
          cell-class-name="cell-class"
          header-row-class-name="header-row-class"
          header-cell-class-name="header-cell-class"
          @selection-change="handleSelectionChange"
        >
          <el-table-column
            prop=""
            label="选择"
            width="154"
            type="selection"
            align="center"
            class-name=""
            label-class-name=""
          >
          </el-table-column>
          <el-table-column
            prop="name"
            label="姓名"
            width="153"
            align="center"
          >
          </el-table-column>
          <el-table-column prop="sex" label="性别" width="154" align="center">
          </el-table-column>
          <el-table-column
            prop="unit"
            label="单位"
            width="153"
            align="center"
          >
          </el-table-column>
          <el-table-column prop="job" label="职务" align="center">
          </el-table-column>
        </el-table>
      </div>
    </div>
    <span slot="footer" class="dialog-footer">
        <button class="green" @click="showYprDialogclosure()">确 定</button>
      </span>
  </el-dialog>
</template>

<script>
import {getSelectJob} from "@/api/propositionReview/approval";
import { getDeptList,selectDetailsJob } from "@/api/courseAchievement/entryResults";

export default {
  props: ["dialogTitle", "showYprDialog"],
  name: "showYprDialog",
  data() {
    return {
      searchNameList: [],
      handleSelection: [],
      namelist: '',
      courseYpr: '',
      yjnameList: [],
      showYprDia: false,
      dw: "",
      dwList: [],
    }
  },
  created() {
  },
  watch: {
    showYprDialog: function (nv) {
      // this.showYprDia=nv
      this.dw = ''
      if (nv && nv != '') {
        this.showYprDialogClick(nv)
      } else {
        this.showYprDialogClick('')
      }
      console.log(nv)

    }
  },
  mounted() {
    getDeptList().then(res=>{
      if(res.data.code == 200){
        this.dwList = res.data.data;
      }
    })
    this.showYprClick()
  },
  methods: {
    delTag(index) {
      let el = this
      console.log(this.$refs.multipleTable, 'multipleTable')
      // this.$refs.multipleTable.clearSelection(this.searchNameList[index]);
      this.searchNameList.splice(index, 1);
      el.$nextTick(() => {
        el.$refs.multipleTable.toggleRowSelection(
          el.yjnameList.find(item => {
              return this.handleSelection[index].id == item.id;
            }
          ), false);

      })
    },
    showYprClick() {
      let teacherInfo = {
        bzbm:this.dw,
        xm:this.courseYpr
      };
      selectDetailsJob(teacherInfo).then(res=>{
        if(res.data.code == 200){
          this.yjnameList = res.data.data;
        }
      })
      // let obj = {
      //   unit: this.dw,
      //   name: this.courseYpr
      // }
      // return new Promise(() => {
      //   getSelectJob(obj).then(res => {
      //     // console.log(res.data, 'res')
      //     console.log("1111111")
      //     this.yjnameList = res.data.data
      //     console.log(this.yjnameList)
      //     console.log("222222")
      //     console.log(res.data.data)
      //   })
      // })
    },
    handleSelectionChange(val) {
      this.handleSelection.splice(0, this.handleSelection.length);
      this.searchNameList.splice(0, this.searchNameList.length);
      this.handleSelection = val
      for (const valKey in val) {
        this.searchNameList[valKey] = val[valKey].name
      }
    },
    showYprDialogclosure() {
      this.showYprDia = false
      let arr1 = []
      let arr2 = []
      for (const Key in this.handleSelection) {
        arr1[Key] = this.handleSelection[Key].id
        arr2[Key] = this.handleSelection[Key].name
      }

      let str1 = arr1.join(",");
      let str2 = arr2.join(",");

      this.$emit('renew', str1,str2)

    },
    showYprDialogClick(rows) {
      let arr = []
      this.$emit('showYprDialClick', '')
      this.showYprDia = true
      this.handleSelection.splice(0, this.handleSelection.length);
      this.searchNameList.splice(0, this.searchNameList.length);
      if (rows.indexOf) {
        arr = rows.split(',')
      } else {
        arr.push(rows)
      }
      this.getSelectJob(arr)
    },
    getSelectJob: function (rows) {
      let obj = {rows}
      let el = this
      return new Promise(() => {
        getSelectJob(obj).then(res => {
          // console.log(res.data, 'res')
          this.yjnameList = res.data.data
          el.$nextTick(() => {
            rows.forEach(row => {

              el.$refs.multipleTable.toggleRowSelection(
                el.yjnameList.find(item => {
                    return row == item.id;
                  }
                ), true);
            });
          })
        })
      })
    },
  },
  computed: {},//计算属性
}
</script>

<style scoped lang="less">
/deep/ .el-dialog {
  display: flex;
  justify-content: flex-start;
  flex-direction: column;
}

.choose-ypr-container {
  width: 767px;
  margin: 0px auto;

  .choose-ypr-search-container {
    .el-input--prefix .el-input__inner {
      padding-left: 30px;
    }

    .el-input__inner {
      &::placeholder {
        color: #b8b8b8;
      }

      &::-webkit-input-placeholder {
        /* WebKit browsers 适配谷歌 */
        color: #b8b8b8;
      }

      &:-moz-placeholder {
        /* Mozilla Firefox 4 to 18 适配火狐 */
        color: #b8b8b8;
      }

      &::-moz-placeholder {
        /* Mozilla Firefox 19+ 适配火狐 */
        color: #b8b8b8;
      }

      &:-ms-input-placeholder {
        /* Internet Explorer 10+  适配ie*/
        color: #b8b8b8;
      }
    }

    display: flex;
    flex-direction: row;
    align-items: center;

    .title {
      font-size: 18px;
      font-family: Microsoft YaHei;
      font-weight: bold;
      line-height: 20px;
      color: #004ca7;
      margin-right: 5px;
    }

    .search-button {
      width: 77px;
      height: 28px;
      line-height: 28px;
      background: #f1f8fe;
      border: 1px solid #51aef6;
      border-radius: 6px;
      cursor: pointer;
      display: flex;
      flex-direction: row;
      align-items: center;
      justify-content: center;

      > span {
        font-size: 16px;
        font-family: Microsoft YaHei;
        font-weight: 400;
        color: #51aef6;
      }

      > img {
        width: 20px;
        height: 20px;
        margin-right: 4px;
      }
    }

    .button-list {
      display: flex;
      flex-direction: row;
      align-items: center;
      margin-left: 21px;

      .button-item {
        display: flex;
        flex-direction: row;
        align-items: center;
        justify-content: center;
        position: relative;
        width: 69px;
        height: 27px;
        line-height: 27px;
        border: 1px solid #004ca7;
        border-radius: 4px;
        margin-right: 10px;
        text-align: center;
        font-size: 14px;
        font-family: Microsoft YaHei;
        font-weight: 400;
        color: #004ca7;

        .del-icon {
          position: absolute;
          right: 0px;
          top: 0px;
          width: 12px;
          height: 12px;
          padding: 2px;
        }
      }
    }
  }

  .choose-ypr-table-container {
    margin-top: 30px;

    .el-table td,
    .el-table th.is-leaf,
    .el-table--border,
    .el-table--group {
      border-color: #c7d7eb;
    }

    .el-table__header tr,
    .el-table__header th {
      padding: 0;
      height: 57px;
      background-color: #edf2f9;
      font-size: 16px;
      font-family: Microsoft YaHei;
      font-weight: bold;
      line-height: 20px;
      color: #004ca7;
    }

    .el-table__body tr,
    .el-table__body td {
      padding: 0;
      height: 51px;
    }

    .table-data {
      width: 100%;
      border: 0.8px solid #004ca7;
      border-radius: 9px;
      font-size: 14px;
      font-family: Microsoft YaHei;
      font-weight: 400;
      color: #004ca7;
    }
  }
}

</style>
